package com.fire.core.service;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import com.fire.core.util.BmiDateUtil;

/**
 * 计算BMI值
 * @author yuanchong
 *
 */
@Service
public class BmiRuleService {
	/**
	 * * 计算BMI值
	 * @param birthday yyyy-MM-dd
	 * @param weight
	 * @param height
	 * @return
	 */
	public boolean calcBMIRule(String birthday, String weight, String height){
		int ages = BmiDateUtil.getYears(birthday, null);
        float w = Float.valueOf(weight);
        float h = Float.valueOf(height);

        boolean boolBMI = false;
        if(ages < 12){
            if(ages < 2){
                int days = BmiDateUtil.getDays(birthday, null);
                if((days < 90) && (h < 46.7 || h > 65.0 || w < 2.5 || w > 7.6)){

                }else if((90 <= days && days < 180) && (h < 57 || h > 71.3 || w < 5.1 || w > 9.8)){

                }else if((180 <= days && days < 300) && (h < 62.7 || h > 77 || w < 6.4 || w > 11.3)){

                }else if((300 <= days && days < 540) && (h < 67.6 || h > 85.6 || w < 7.1 || w > 13.0)){

                }else if((540 <= days && days < 720) && (h < 74.4 || h > 92 || w < 8.3 || w > 14.4)){

                }else{
                    boolBMI = true;
                }
            }else{
                if((ages < 4) && (h < 79.8 || h > 103.7 || w < 9.5 || w > 17.6)){

                }else if((4 <= ages && ages < 6) && (h < 92.8 || h > 118.2 || w < 12.4 || w > 22.9)){

                }else if((6 <= ages && ages < 9) && (h < 104.3 || h > 139.8 || w < 15.3 || w > 37.6)){

                }else if((9 <= ages) && (h < 117.9 || h > 166.4 || w < 19.2 || w > 63.5)){

                }else{
                    boolBMI = true;
                }
            }
        }else{
            BigDecimal wBig = new BigDecimal(weight);
            BigDecimal hBig = new BigDecimal(height);
            BigDecimal resBig = wBig.divide(hBig.multiply(hBig).divide(new BigDecimal(10000)), 2, BigDecimal.ROUND_HALF_UP);
            if((ages > 16 && h <= 140) || (resBig.floatValue() <= 16.0 || resBig.floatValue() >= 27.0)){

            }else{
                boolBMI = true;
            }
        }
        //true:BMI在正常范围      false:BMI不合格
    	return boolBMI;
    }
	
	
	/**
	 * 入参校验
	 * @param birthday  yyyy-MM-dd
	 * @param weight    float
	 * @param height    float
	 * @return
	 */
	public String checkReqParam(String birthday, String weight, String height) {
		String tipStr = "";
		if (StringUtils.isEmpty(birthday)) {
			tipStr = "生日不可为空";
			return tipStr;
		}
		try {
            DateUtils.parseDate(birthday, "yyyy-MM-dd");
        } catch (Exception e) {
			tipStr = "必须传入yyyy-MM-dd的日期字符串";
			return tipStr;
        }
		if (StringUtils.isEmpty(weight)) {
			tipStr = "体重不可为空";
			return tipStr;
        }
		try {
			Float.valueOf(weight);
		}catch(Exception e) {
			tipStr = "体重参数格式错误";
			return tipStr;
		}
		if (StringUtils.isEmpty(height)) {
			tipStr = "身高不可为空";
			return tipStr;
		}
		try {
			Float.valueOf(height);
		}catch(Exception e) {
			tipStr = "身高参数格式错误";
			return tipStr;
		}
		return tipStr;
	}
}
